<template>
  <div class="container">
    <div class="container-title">
      <p>行业类型</p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>招商管理</el-breadcrumb-item>
        <el-breadcrumb-item>招商配置</el-breadcrumb-item>
        <el-breadcrumb-item>行业类型</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="search-box">
      <el-form ref="form" :model="searchData">
        <el-form-item
          ><el-input
            v-model="searchData.business"
            placeholder="大类名称"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="success"
            @click="onSearch"
            icon="el-icon-search"
            style="background-color: #0bb197"
            >查询</el-button
          ><el-button @click="onReset" icon="el-icon-refresh" class="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div class="data-list">
      <div class="data-list-title">
        <span>查询结果列表</span>
        <el-button
          type="success"
          style="background-color: #0bb197"
          @click="addOpen"
          >新增</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column
          prop="businessCoding"
          label="行业大类编码"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="business" label="行业大类" align="center">
        </el-table-column>
        <el-table-column prop="childNum" label="小类项数" align="center">
        </el-table-column>
        <el-table-column prop="buisnessRemark" label="备注" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button @click="editOpen(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button @click="delOpen(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template></el-table-column
        >
      </el-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="searchData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
      >
      </el-pagination>
    </div>

    <!-- 新增弹窗 -->
    <el-dialog
      title="新增行业类型"
      :visible.sync="addDialog"
      width="43%"
      class="add"
      top="8vh"
    >
      <!-- 新增小类弹窗 -->
      <el-dialog
        width="43%"
        title="新增小类"
        :visible.sync="addChildDialog"
        append-to-body
        class="newPlan"
      >
        <el-form :model="addChildData">
          <div class="code-name">
            <el-form-item label="小类代码">
              <el-input
                v-model="addChildData.childCoding"
                autocomplete="off"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
            <el-form-item label="小类名称">
              <el-input
                v-model="addChildData.childName"
                autocomplete="off"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </div>
          <div class="remarks">
            <el-form-item label="备注">
              <el-input
                v-model="addChildData.childRemark"
                autocomplete="off"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addChildDialog = false" class="cancel"
            >取 消</el-button
          >
          <el-button type="primary" @click="addChildSave" class="save"
            >保 存</el-button
          >
        </div>
      </el-dialog>

      <el-form :model="addData">
        <div class="code-name">
          <el-form-item label="行业代码">
            <el-input
              ref="getvalue"
              @blur="getChildTable()"
              v-model="addData.businessCoding"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="行业大类名称">
            <el-input
              v-model="addData.business"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </div>
        <div class="remarks">
          <el-form-item label="备注">
            <el-input
              v-model="addData.buisnessRemark"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div class="plan-list">
        子类信息：
        <div class="new-plan-btn">
          <el-button type="primary" @click="addSave">新增小类</el-button>
        </div>
      </div>
      <el-table :data="childTableData" stripe>
        <el-table-column
          property="id"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column
          property="childCoding"
          label="小类编码"
          align="center"
        ></el-table-column>
        <el-table-column
          property="childName"
          label="小类名称"
          align="center"
        ></el-table-column
        ><el-table-column
          property="childRemark"
          label="备注"
          align="center"
        ></el-table-column
        ><el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="childDelOpen(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false" class="cancel">取 消</el-button>
        <el-button type="primary" @click="addDialog = false" class="save"
          >保 存</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑弹窗 -->
    <el-dialog
      title="编辑行业类型"
      :visible.sync="editDialog"
      width="43%"
      class="edit"
      top="8vh"
    >
      <!-- 编辑新增小类弹窗 -->
      <el-dialog
        width="43%"
        title="新增小类"
        :visible.sync="editPlanDialog"
        append-to-body
        class="editPlan"
      >
        <el-form :model="editChildData">
          <div class="code-name">
            <el-form-item label="小类代码">
              <el-input
                v-model="editChildData.childCoding"
                autocomplete="off"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
            <el-form-item label="小类名称">
              <el-input
                v-model="editChildData.childName"
                autocomplete="off"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </div>

          <div class="remarks">
            <el-form-item label="备注">
              <el-input
                v-model="editChildData.childRemark"
                autocomplete="off"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editPlanDialog = false" class="cancel"
            >取 消</el-button
          >
          <el-button type="primary" @click="editChildSave" class="save"
            >保 存</el-button
          >
        </div>
      </el-dialog>

      <el-form :model="editData">
        <div class="code-name">
          <el-form-item label="行业代码">
            <el-input
              ref="getvalue"
              @blur="getChildTable()"
              :disabled="true"
              v-model="editData.businessCoding"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="行业大类名称">
            <el-input
              v-model="editData.business"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </div>
        <div class="remarks">
          <el-form-item label="备注">
            <el-input
              v-model="editData.buisnessRemark"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div class="plan-list">
        子类信息：
        <div class="new-plan-btn">
          <el-button type="primary" @click="editSave"> 新增小类</el-button>
        </div>
      </div>
      <el-table :data="editTableData" stripe>
        <el-table-column
          property="id"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column
          property="childCoding"
          label="小类编码"
          align="center"
        ></el-table-column>
        <el-table-column
          property="childName"
          label="小类名称"
          align="center"
        ></el-table-column>
        <el-table-column
          property="childRemark"
          label="备注"
          align="center"
        ></el-table-column
        ><el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="editDelOpen(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false" class="cancel">取 消</el-button>
        <el-button type="primary" @click="editDialog = false" class="save"
          >保 存</el-button
        >
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <el-dialog :visible.sync="delDialog" width="21%" center class="del">
      <span>数据删除将无法恢复，请问确认删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialog = false" class="cancel">取 消</el-button>
        <el-button type="primary" @click="delSave" class="save"
          >保 存</el-button
        >
      </span>
    </el-dialog>
    <!-- 新增子删除弹窗 -->
    <el-dialog :visible.sync="childDelDialog" width="21%" center class="del">
      <span>数据删除将无法恢复，请问确认删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialog = false" class="cancel">取 消</el-button>
        <el-button type="primary" @click="childDel" class="save"
          >保 存</el-button
        >
      </span>
    </el-dialog>
    <!-- 编辑子删除弹窗 -->
    <el-dialog
      :visible.sync="editChildDelDialog"
      width="21%"
      center
      class="del"
    >
      <span>数据删除将无法恢复，请问确认删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialog = false" class="cancel">取 消</el-button>
        <el-button type="primary" @click="editChildDel" class="save"
          >保 存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import time from '../utils/time';
export default {
  mixins: [time],
  data: function () {
    return {
      total: 0,
      currentPage: 1,
      options: [
        {
          value: '0',
          label: '已完成',
        },
        {
          value: '1',
          label: '发布中',
        },
      ],
      //搜索数据
      searchData: {
        pageNum: 1,
        pageSize: 5,
        business: '',
      },
      //渲染数据
      tableData: [],
      //弹窗子表数据
      childTableData: [],
      //编辑弹窗子表数据
      editTableData: [],
      //新增弹窗
      addDialog: false,
      addChildDialog: false,
      editDialog: false,
      editPlanDialog: false,
      delDialog: false,
      childDelDialog: false,
      editChildDelDialog: false,
      //新增数据
      addData: {
        buisnessRemark: '',
        business: '',
        businessCoding: 0,
      },
      addChildData: {
        childCoding: '',
        childName: '',
        childRemark: '',
        businessTypeCoding: 0,
      },

      editData: {
        buisnessRemark: '',
        business: '',
        businessCoding: 0,
      },
      editChildData: {
        childCoding: '',
        childName: '',
        childRemark: '',
        businessTypeCoding: 0,
      },
      //删除id
      delId: 0,
      delChildId: 0,
      editDelId: 0,
    };
  },
  methods: {
    //搜索+渲染
    getData: function () {
      this.$axios({
        url: '/smart/business/type/list',
        params: this.searchData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          if (data.data.list.length > 0) {
            this.tableData = data.data.list;
            this.total = data.data.total;
          } else {
            this.tableData = data.data.list;
            this.$message.error('暂无数据！');
          }
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //搜索
    onSearch: function () {
      this.getData();
    },
    //重置
    onReset: function () {
      this.searchData = {
        pageNum: 1,
        pageSize: 10,
        business: '',
      };
      this.getData();
    },
    //新增弹窗
    addOpen: function () {
      this.addData = {
        buisnessRemark: '',
        business: '',
        businessCoding: '',
      };
      this.addDialog = true;
    },
    //打开table渲染
    getChildTable: function () {
      this.$axios({
        url: '/smart/business/type/list/child/' + this.$refs.getvalue.value,
      }).then(({ data }) => {
        if (data.code == '00000') {
          if (data.data.length > 0) {
            this.childTableData = data.data;
          } else {
            this.childTableData = data.data;
            this.$message.error('暂无数据');
          }
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //父增加
    addSave: function () {
      this.addChildData = {
        childCoding: '',
        childName: '',
        childRemark: '',
      };
      this.addData.businessCoding = Number(this.addData.businessCoding);
      this.addChildDialog = true;
      this.$axios({
        url: '/smart/business/type/add',
        method: 'post',
        data: this.addData,
      }).then(({ data }) => {
        if (data.code == '00000') {
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //子增加
    addChildSave: function () {
      this.$axios({
        url: '/smart/business/type/add/child',
        method: 'post',
        data: {
          businessTypeCoding: this.addData.businessCoding,
          childCoding: this.addChildData.childCoding,
          childName: this.addChildData.childName,
          childRemark: this.addChildData.childRemark,
        },
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.addChildDialog = false;
          this.$message.success(data.msg);
          this.getData();
          this.getChildTable();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //编辑
    //编辑弹窗table渲染
    editTable: function () {
      this.$axios({
        url: '/smart/business/type/list/child/' + this.editData.businessCoding,
      }).then(({ data }) => {
        if (data.code == '00000') {
          if (data.data.length > 0) {
            this.editTableData = data.data;
          } else {
            this.editTableData = data.data;
            this.$message.error('暂无数据！');
          }
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //编辑弹窗打开
    editOpen: function (row) {
      this.editDialog = true;
      this.$axios({
        url: '/smart/business/type/typeall',
        method: 'post',
        params: { coding: row.businessCoding },
      }).then(({ data }) => {
        if (data.code == '00000') {
          data.data.forEach((v) => {
            if (v.id == row.id) {
              this.editData = v;
              this.editChildData = v;
              this.editTableData = [v];
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //父编辑保存
    editSave: function () {
      this.editPlanDialog = true;
      this.$axios({
        url: '/smart/business/type/update',
        method: 'put',
        data: {
          id: this.editData.id,
          buisnessRemark: this.editData.buisnessRemark,
          business: this.editData.business,
          businessCoding: this.editData.businessCoding,
        },
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.$message.success(data.msg);
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //子编辑保存
    editChildSave: function () {
      this.$axios({
        url: '/smart/business/type/update/child',
        method: 'put',
        data: {
          id: this.editChildData.id,
          businessTypeCoding: this.editChildData.businessTypeCoding,
          childCoding: this.editChildData.childCoding,
          childName: this.editChildData.childName,
          childRemark: this.editChildData.childRemark,
        },
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.editPlanDialog = false;
          this.$message.success(data.msg);
          this.getChildTable();
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //删除弹窗
    delOpen: function (row) {
      this.delDialog = true;
      this.delId = row.id;
    },
    //删除保存
    delSave: function () {
      this.$axios({
        url: '/smart/business/type/del/' + this.delId + '}',
        method: 'DELETE',
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.delDialog = false;
          this.$message.success(data.msg);
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //增加删除弹窗
    childDelOpen: function (row) {
      this.childDelDialog = true;
      this.delChildId = row.id;
    },
    //增加弹窗table删除
    childDel: function () {
      this.$axios({
        url: '/smart/business/type/del/child/' + this.delChildId + '}',
        method: 'delete',
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.childDelDialog = false;
          this.getChildTable();
          this.$message.success(data.msg);
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //编辑删除弹窗
    editDelOpen: function (row) {
      this.editChildDelDialog = true;
      this.editDelId = row.id;
    },
    //编辑table删除保存
    editChildDel: function () {
      this.$axios({
        url: '/smart/business/type/del/child/' + this.editDelId + '}',
        method: 'delete',
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.$message.success(data.msg);
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //页码
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.searchData.pageNum = val;
      this.getData();
    },
  },
  mounted: function () {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  .container-title {
    width: 98%;
    height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    p {
      color: #333333;
      font-size: 16px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
    .el-breadcrumb-item {
      color: rgba(148, 148, 148, 0.898039215686275);
      font-size: 12px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
  }
  .search-box {
    width: 98%;
    height: 80px;
    background: #ffffff;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    border: 1px solid #ffffff;
    border-radius: 5px;
    .el-form {
      width: 98%;
      background: #ffffff;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .el-form-item {
        margin-left: 10px;
        margin-bottom: 0;
        .el-select {
          width: 155px;
        }
        .el-button {
          margin: 10px;
        }
        .reset {
          background-color: white;
          color: #c0c4cc;
          border: 1px solid #c0c4cc;
        }
        .reset:hover {
          color: #0bb197;
        }
      }
      .block {
        margin-left: 10px;
      }
      .el-date-editor--datetimerange.el-input__inner {
        width: 310px;
      }
    }
  }
  .data-list {
    width: 98%;
    height: 100%;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    margin-top: 20px;
    .data-list-title {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        height: 16px;
        font-family: 'Arial Normal', 'Arial';
        font-weight: 700;
        font-style: normal;
        font-size: 13px;
        color: #3c4145;
      }
    }
    .el-table {
      flex: 1;
      .el-button {
        color: #0bb197;
      }
    }
    .el-pagination {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
}
</style>
<style lang="less">
.el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #0bb197;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #0bb197;
  color: #fff;
}
.edit .el-dialog,
.add .el-dialog {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 95%;
    height: 60px;
    padding: 0;
    .el-dialog__title {
      font-size: 15px;
      font-weight: bold;
      text-align: left;
      line-height: 60px;
    }
  }
  .el-dialog__body {
    width: 95%;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    .el-form {
      width: 95%;
      display: flex;
      flex-direction: column;
      align-items: space-between;
      .code-name,
      .remarks {
        width: 95%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .el-form-item {
          width: 48%;
        }
      }
    }
    .plan-list {
      width: 95%;
      height: 80px;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: space-between;
      .new-plan-btn {
        width: 100%;
        height: 50px;
        border-top: 1px solid #0bb197;
        display: flex;
        align-items: center;
        .el-button {
          width: 66px;
          height: 30px;
          display: flex;
          justify-content: center;
          align-items: center;
          color: #ffffff;
          background: #0bb197;
        }
      }
    }
    .el-table {
      width: 95%;
      .el-button {
        color: #0bb197;
      }
    }
  }
  .el-dialog__footer {
    width: 95%;
    height: 80px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .dialog-footer {
      width: 50%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .save {
        width: 66px;
        height: 30px;
        background: #0bb197;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .cancel {
        width: 66px;
        height: 30px;
        background: #dfe1e8;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .save:focus,
      .save:hover {
        color: #ffffff;
        border-color: #c6e2ff;
      }
      .cancel:focus,
      .cancel:hover {
        color: #333333;
        border-color: #dfe1e8;
      }
    }
  }
}
.editPlan .el-dialog,
.newPlan .el-dialog {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 95%;
    height: 60px;
    padding: 0;
    .el-dialog__title {
      line-height: 60px;
      font-size: 15px;
      font-weight: bold;
    }
  }
  .el-dialog__body {
    width: 95%;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    .el-form {
      width: 95%;
      display: flex;
      flex-direction: column;
      align-items: center;
      .code-name {
        width: 95%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .el-form-item {
          width: 48%;
        }
        .el-date-editor.el-input,
        .el-date-editor.el-input__inner {
          width: 100%;
        }
      }
      .remarks {
        width: 95%;
      }
    }
  }
  .el-dialog__footer {
    width: 95%;
    height: 80px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .dialog-footer {
      width: 50%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .save {
        width: 66px;
        height: 30px;
        background: #0bb197;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .cancel {
        width: 66px;
        height: 30px;
        background: #dfe1e8;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .save:focus,
      .save:hover {
        color: #ffffff;
        border-color: #c6e2ff;
      }
      .cancel:focus,
      .cancel:hover {
        color: #333333;
        border-color: #dfe1e8;
      }
    }
  }
}
.del .el-dialog {
  .el-dialog__body {
    display: flex;
    justify-content: center;
  }
  .el-dialog__footer {
    width: 95%;
    height: 80px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .dialog-footer {
      width: 50%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .save {
        width: 66px;
        height: 30px;
        background: #0bb197;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .cancel {
        width: 66px;
        height: 30px;
        background: #dfe1e8;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .save:focus,
      .save:hover {
        color: #ffffff;
        border-color: #c6e2ff;
      }
      .cancel:focus,
      .cancel:hover {
        color: #333333;
        border-color: #dfe1e8;
      }
    }
  }
}
</style>
